/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controleur;
import javax.xml.parsers.*;
 import org.xml.sax.*;
 import org.xml.sax.helpers.*;

 public class SAXReader extends DefaultHandler {

 protected String text;

 public SAXReader() throws Exception {
 super();
     System.out.println("super");
 read("./src/Exercice2.xml");

 }

 // creation du parser
 public void read(String fileName) {
 try {
 XMLReader parser = XMLReaderFactory.createXMLReader();
 parser.setContentHandler(this);
 //parser.setFeature("http://xml.org/sax/features/validation", true);
 parser.parse(fileName);
     System.out.println("read");
 } catch (Exception e) {
 System.err.println(e.getMessage());
 }
 }

 @Override
 public void characters(char[] ch, int start, int length)
 throws SAXException {
 text = new String(ch, start, length);
     System.out.println(text);
}
 
 // read <tag>value</tag>
 @Override
 public void endElement(String uri, String localName, String qName)
 throws SAXException {

 if (localName.equals("prenom")) {

 String value = text.trim();

     System.out.println(value);
 }
}

 // read <tag attr=”vattr” />
 public void startElement(String uri, String localName, String qName,
 Attributes atts) throws SAXException {

if (localName.equals("tag")) {

 String vattr = atts.getValue("attr").trim();

// note : be careful because atts.getValue will return null
 // if attribute is not present
 if (vattr==null) {
 vattr="";
 }
 }

 }
 }




